#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
 
#define FIB_UNTIL 10
uv_loop_t *loop;

typedef struct {
    uv_work_t req;
    int in;
    int out;
} req_data_t;

long fib_(long t)
{
    if (t == 0 || t == 1)
        return 1;
    else
        return fib_(t-1) + fib_(t-2);
}
 
void fib(uv_work_t *req)
{
    req_data_t *req_data = (req_data_t *)req;
    req_data->out = fib_(req_data->in);
    uv_sleep(1000);
}
 
void after_fib(uv_work_t *req, int status)
{
    fprintf(stderr, "fibonacci %d is %d\n", ((req_data_t *)req)->in, ((req_data_t *)req)->out);
}

int main()
{
    loop = uv_default_loop();
 
    req_data_t req_data[FIB_UNTIL];
    for (int i = 0; i < FIB_UNTIL; i++)
    {
        req_data[i].in = i;
        uv_queue_work(loop, (uv_work_t *)&req_data[i], fib, after_fib);
    }
 
    uv_run(loop, UV_RUN_DEFAULT);
    uv_loop_close(loop);
    return 0;
}
